﻿Public Class ChuNhaDAO
    Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect
    End Sub
    Public Function CapNhat(ByVal emp As ChuNhaDTO, ByVal flag As Boolean)
        Dim cmnd As String = emp.CMNDChuNha
        Dim ten As String = emp.TenChuNha
        Dim diachi As String = emp.DiaChiChuNha
        Dim sodt As String = emp.DienThoaiChuNha
        Dim maloainha As String = emp.MaLoaiNha
        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT THONGTINCHUNHA (CMNDChuNha, TenChuNha, DiaChiChuNha, DienThoaiChuNha, MaLoaiNha)" & _
                        " VALUES('" & cmnd & "', '" & ten & "', '" & _
                                        diachi & "', '" & sodt & "', '" & maloainha & "')"
        Else
            sqlstr = "UPDATE THONGTINCHUNHA SET  CMNDChuNha  = '" & cmnd & _
                                        "', TenChuNha   = '" & ten & _
                                        "', DiaChiChuNha      = '" & diachi & _
                                        "',  DienThoaiChuNha      = '" & sodt & _
                                         "', MaLoaiNha      = '" & maloainha & _
                                    "' WHERE CMNDChuNha  = '" & cmnd & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function
    Public Function XoaChuNha(ByVal emp As ChuNhaDTO)
        Dim cmnd As String = emp.CMNDChuNha()
        sqlstr = "DELETE THONGTINCHUNHA WHERE CMNDChuNha = '" & cmnd & "'"
        Return connect.Execute(sqlstr)
    End Function

    Public Function GetData() As List(Of ChuNhaDTO)
        Dim i As Integer
        Dim cmnd, ten, diachi, sdt, maloainha As String
        Dim cnDTO As ChuNhaDTO
        Dim cnList As New List(Of ChuNhaDTO)
        Dim dt As New DataTable
        dt = connect.GetData("select * from THONGTINCHUNHA")
        For i = 0 To dt.Rows.Count() - 1
            cmnd = dt.Rows(i)("CMNDChuNha").ToString
            ten = dt.Rows(i)("TenChuNha").ToString
            diachi = dt.Rows(i)("DiaChiChuNha").ToString
            sdt = dt.Rows(i)("DienThoaiChuNha").ToString
            maloainha = dt.Rows(i)("MaLoaiNha").ToString
            cnDTO = New ChuNhaDTO(cmnd, ten, diachi, sdt, maloainha)
            cnList.Add(cnDTO)
        Next
        Return cnList
    End Function
End Class
